<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="listQuery.start_begin_time"
        type="month"
        class="filter-item"
        value-format="yyyy-MM-dd"
        placeholder="开班开始日期"
      ></el-date-picker>
      <el-date-picker
        v-model="listQuery.start_end_time"
        type="month"
        class="filter-item"
        value-format="yyyy-MM-dd"
        placeholder="开班结束日期"
      ></el-date-picker>
      <el-input
        v-model="listQuery.search"
        placeholder="班级名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :loading="listLoading"
        @click="handleFilter"
      >搜索</el-button>
      <a style="position:absolute; right:40px;top:110px;color:blue;" href="https://xy.bufanui.com/feedback" target="_blank">反馈表页面</a>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      @sort-change="sortChange"
    >
      <el-table-column type="index" width="60" align="center" sortable="custom" />
      <el-table-column
        label="开班时间"
        min-width="120"
        prop="class_start_time"
        align="center"
        sortable="custom"
      >
        <template slot-scope="{row}">
          <span>{{ row.class_start_time | dateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年份" prop="year" align="center" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.year }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级" prop="class_name" align="center" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.class_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="反馈数量" align="center">
        <template slot-scope="{row}">
          <span>{{ row.feedback_number }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="100"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <el-button
            size="mini"
            icon="el-icon-document"
            type="primary"
            @click="handleDetail(row)"
          >反馈</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.page_size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList } from '@/api/classes'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'FeedbackClassList',
  components: { Pagination },
  directives: { waves },
  filters: {
    dateFormat(time) {
      return parseTime(time, '{y}-{m}-{d}')
    }
  },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        feedback_number: 1,
        page: 1,
        page_size: 10,
        start_begin_time: undefined,
        start_end_time: undefined,
        search: undefined,
        order: '-id'
      },
      downloadLoading: false
    }
  },
  created() {
    this.listQuery.class_type = this.$route.meta.class_type
    this.getList()
  },
  methods: {
    async getList() {
      try {
        this.listLoading = true
        const { data } = await fetchList(this.listQuery)
        const { data: list, total } = data
        this.list = list
        this.total = total
        this.listLoading = false
      } catch (e) {
        this.listLoading = false
      }
    },
    handleDetail(row) {
      const { class_name, feedback_number, id } = row
      const type = this.listQuery.class_type === 1 ? 'UI' : '前端'
      this.$router.push({
        name: 'FeedbackDetail',
        query: {
          id,
          feedback_number,
          className: `${type}-${class_name}`
        }
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        this.sort(prop, order)
      } else {
        this.listQuery.sort = '-id'
        this.handleFilter()
      }
    },
    sort(prop, order) {
      if (order === 'ascending') {
        this.listQuery.sort = `+${prop}`
      } else {
        this.listQuery.sort = `-${prop}`
      }
      this.handleFilter()
    }
  }
}
</script>
